package com.yjd.comm.user.util;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;

import com.yjd.comm.user.dto.UserSession;
import com.yjd.comm.user.enums.ClientChannelEnum;
import com.yjd.comm.user.model.UserModel;
import com.yjd.comm.util.ServletUtil;
import com.yjd.comm.util.StringUtil;
import com.yjd.comm.vo.CachedKeys;

/**
 * @ClassName: UserUtil
 * @Description: 会员工具类
 * @author ready likun_557@163.com
 * @date 2014-8-16 下午3:01:41
 */
public class UserUtil {

	/**
	 * 获取登陆的会员信息
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	public static UserSession getUserModel(HttpServletRequest request,
			HttpServletResponse response, ClientChannelEnum clientChannelEnum) {
		UserSession userSession = (UserSession) request
				.getAttribute(CachedKeys.LOGIN_USER);
		if (clientChannelEnum == ClientChannelEnum.PIC) {
			if (userSession == null) {
				userSession = (UserSession) ServletUtil.getSession(request,
						response, CachedKeys.LOGIN_USER);
			}
		}
		return userSession;
	}

	public static void putSessionUserModel(HttpServletRequest request,
			HttpServletResponse response, UserModel userModel) {
		ServletUtil.putSession(request, response, CachedKeys.LOGIN_USER,
				userModel);
	}

	/**
	 * @Title: disposeIdCard
	 * @Description: 身份证号码显示处理
	 * @param @param idCard
	 * @param @return 设定文件
	 * @return String 返回类型
	 * @throws
	 */
	public static String disposeIdCard(String idCard) {
		if (StringUtil.isEmpty(idCard)) {
			return null;
		} else {
			if (idCard.length() > 6) {
				return idCard.substring(0, 6)
						+ StringUtil.replaceChar(idCard.substring(6), "*");
			} else {
				return idCard;
			}
		}
	}
	
	
	/**
	 * 工资卡卡号处理
	* @param salaryCardNo
	* @return String
	 */
	public static String disposeSalaryCard(String salaryCardNo) {
		if (StringUtils.isBlank(salaryCardNo)) {
			return null;
		} else {
			if (salaryCardNo.length() == 16) {
				return salaryCardNo.substring(0, 4)
						+ StringUtil.replaceChar(salaryCardNo.substring(4), "*") + salaryCardNo.substring(salaryCardNo.length() - 4);
			} else {
				return salaryCardNo;
			}
		}
	}
	
	/**
	 * 
	* 银行卡处理
	* @date 2015年11月20日 下午2:47:40
	* @param bankCardNo
	* @return
	* String
	 */
	public static String disposeBankCardNo(String bankCardNo) {
		if (StringUtils.isBlank(bankCardNo)) {
			return null;
		} 
		int length = bankCardNo.length() - 4;
		if(length <= 0){
			return bankCardNo;
		}
		return StringUtil.replaceChar(bankCardNo.substring(0, length), "*") +  bankCardNo.substring(length);
	}
	
	/**
	 * 
	 * @Title: disposeEmail
	 * @Description: 邮件地址处理
	 * @param mobile
	 * @return String
	 */
	public static String disposeEmail(String email) {
		if (StringUtils.isBlank(email)) {
			return email;
		}
		return StringUtils.replaceOnce(email,
				email.substring(2, email.indexOf("@")), "******");
	}

	/**
	 * 
	 * @Title: disposeMobile
	 * @Description: 手机号码处理
	 * @param mobile
	 * @return String
	 */
	public static String disposeMobile(String mobile) {
		if (StringUtils.isBlank(mobile)) {
			return mobile;
		}
		if (mobile.length() < 11) {
			return mobile;
		}
		return StringUtils.replaceOnce(mobile, mobile.substring(3, 7), "****");
	}

	/**
	 * 处理用户名
	 * 
	 * @param userName
	 * @return
	 */
	public static String disposeUserName(String userName) {
		if (StringUtil.isNotEmpty(userName)) {
			StringBuilder builder = new StringBuilder();
			builder.append(userName.charAt(0));
			builder.append("***");
			if (userName.length() >= 2) {
				builder.append(userName.charAt(userName.length() - 1));
			}
			return builder.toString();
		} else {
			return null;
		}
	}
	
	/**
	 * 处理用户名
	 * 
	 * @param userName
	 * @return
	 */
	public static String disposeUserName1(String userName) {
		String um = disposeUserName(userName);
		if(StringUtil.isNotEmpty(um) && um.length()>=2){
			return um.substring(1);
		}
		return null;
	}

}
